# coding=utf-8
import unittest
import os, sys

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.rent_order import RentOrder
from api.base_api.rent_order_waiting import WaitingRentOrder
from fixture import user
import datetime
import random


class RentOrderTest(LoginBaseApi):
    ''' 测试发布租车 '''

    def setUp(self):
        self.rent_order_url = "http://192.168.1.141:8001/rent-order"
        self.renting_contract_mobile = '15011353321'
        self.none_contract_mobile = '13651020524'
        self.start_time = datetime.datetime.now()
        self.end_time = self.start_time + datetime.timedelta(hours=random.randint(1, 240))
        self.rent_day = ((self.end_time - self.start_time).days)+1
        self.rent_amount = random.randint(1, 200)
        self.unit_price = self.rent_day * self.rent_amount
        self.password = '123abc'

    def tearDown(self):
        user.update_rent_order()

    def test_rent_order_success(self):
        ''' 发布租车成功 '''

        #发布租车前获取租车列表
        waiting_rent_order_response = WaitingRentOrder().waiting_rent_order()
        rent_order_list_amount = len(waiting_rent_order_response.json()['results'])

        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        payload = RentOrder().rent_order(str(self.start_time), str(self.end_time), self.rent_day, self.rent_amount,
                                         self.unit_price)
        response = self.post(self.rent_order_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 201)

        #验证租车列表数量
        new_waiting_rent_order_response = WaitingRentOrder().waiting_rent_order()
        new_rent_order_list_amount = len(new_waiting_rent_order_response.json()['results'])
        self.assertEqual(rent_order_list_amount, new_rent_order_list_amount-1)

        waiting_rent_order_id = new_waiting_rent_order_response.json()['results'][0]['id']
        rent_order_id = response.json()['id']
        self.assertEqual(waiting_rent_order_id, rent_order_id)

        waiting_rent_order_username = new_waiting_rent_order_response.json()['results'][0]['creator_username']
        rent_order_username = response.json()['creator_username']
        self.assertEqual(waiting_rent_order_username, rent_order_username)

        waiting_rent_order_status = new_waiting_rent_order_response.json()['results'][0]['rent_status']
        rent_order_status = response.json()['rent_status']
        self.assertEqual(waiting_rent_order_status, rent_order_status)

        waiting_rent_order_unit_price = new_waiting_rent_order_response.json()['results'][0]['unit_price']
        rent_order_unit_price = response.json()['unit_price']
        self.assertEqual(waiting_rent_order_unit_price, rent_order_unit_price)

    def test_again_rent_order(self):
        ''' 租车成功之后再次申请 '''
        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        payload = RentOrder().rent_order(str(self.start_time), str(self.end_time), self.rent_day, self.rent_amount, self.unit_price)
        response = self.post(self.rent_order_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 201)
        # 再次申请
        again_response = self.post(self.rent_order_url, payload, header)
        again_status_code = again_response.status_code
        self.assertIsNot(again_status_code, 200)
        message = again_response.json()['message']
        self.assertEqual(message, '您已经发布过租车信息')

    def test_none_contract_rent_order(self):
        ''' 没有合同用户 '''
        header = LoginBaseApi().login_base_api(self.none_contract_mobile, self.password)
        payload = RentOrder().rent_order(str(self.start_time), str(self.end_time), self.rent_day, self.rent_amount,self.unit_price)
        response = self.post(self.rent_order_url, payload, header)
        status_code = response.status_code
        self.assertIsNot(status_code, 200)